<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: update_urls.php 09594 2018-09-04 09:29:53 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：批量更新URL
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

class update_urls extends base {

    /**
     * 获取model和category基本信息
     */
    public function index() {
        $modelid = 0;
        $modelinfo = get_modelinfo();

        $select = select_category('catids[]', '0', '≡ 所有栏目 ≡', 0, 'multiple="multiple" style="height:200px;width:140px;"', false, false);

        $this->assign('title', '批量更新URL');
        $this->assign('modelid', $modelid);
        $this->assign('modelinfo', $modelinfo);
        $this->assign('select', $select);
        $this->display('update_urls_list');
    }

    /**
     * 更改model
     */
    public function change_model() {
        $modelid = isset($_POST['modelid']) ? intval($_POST['modelid']) : 0;
        $modelinfo = get_modelinfo();
        $select = select_category('catids[]', '0', '≡ 所有栏目 ≡', 0, 'multiple="multiple" style="height:200px;width:140px;"', false, false, $modelid);
        $this->assign('title', '批量更新URL');
        $this->assign('modelid', $modelid);
        $this->assign('modelinfo', $modelinfo);
        $this->assign('select', $select);
        $this->display('update_urls_list');
    }

    /**
     * 更新栏目URL
     */
    public function update_category_url() {
        if (isset($_POST['dosubmit'])) {
            $catids = isset($_POST['catids']) && is_array($_POST['catids']) ? $_POST['catids'] : array('0');
            //根据系统URL规则生成栏目URL
            $url_rule = get_config('url_rule');
            //更新所有栏目
            if (!$catids[0]) {
                $catinfo = get_category();
            } else {
                $catids = join(',', array_map('intval', $catids));
                $catinfo = db('category')->field('catid,catname,type,category_urlrule,catdir')->where("catid IN ($catids)")->select();
            }
            foreach ($catinfo as $val) {
                if ($val['type'] == 2) {
                    continue;  //如果是外部链接则跳过
                }
                //如果系统设置是伪静态模式
                if ($url_rule) {
                    $pclink = URL_MODEL == 1 ? 'index.php?s=/' . $val['catdir'] . '/' : '/' . $val['catdir'] . '/';
                } else {
                    $pclink = url('index/index/lists', 'catid=' . $val['catid']);
                }
                db('category')->where(['catid' => $val['catid']])->update(array('pclink' => $pclink));
            }
            cache('categoryinfo', null);
            showmsg(L('operation_success'), '', 1);
        }
    }

    /**
     * 更新内容页URL
     */
    public function update_content_url() {
        $modelid = isset($_POST['modelid']) ? intval($_POST['modelid']) : (isset($_GET['modelid']) ? intval($_GET['modelid']) : 0);
        $modelinfo = get_modelinfo();
        //当选择所有模型时，则更新所有内容
        if (!$modelid) {
            $i = isset($_GET['i']) ? intval($_GET['i']) : 0;
            $num = count($modelinfo);
            if ($i >= $num) {
                showmsg('更新完成！', '/admin/update_urls', 1);
            }
            $r = db('content')->field('catid,contentid as id')->limit('1000')->select(); //防止数据过多，暂且取前1000条
            foreach ($r as $val) {
                $url = $this->get_url($val['catid'], $val['id']);
                db('content')->where(['contentid' => $val['id']])->update(array('url' => $url));
            }
            showmsg($modelinfo[$i]['name'] . '更新完成...', url('/admin/update_urls/update_content_url', array('i' => ++$i, 'modelid' => 0)), 1);
        } else {
            $modelarr = array();
            foreach ($modelinfo as $val) {
                $modelarr[$val['modelid']] = $val;
            }
            if (!isset($modelarr[$modelid])) {
                showmsg('模型不存在！');
            }
            $r = db('content')->field('catid,contentid as id')->where(['modelid' => $modelid])->limit('1000')->select(); //防止数据过多，暂且取前1000条
            foreach ($r as $val) {
                $url = $this->get_url($val['catid'], $val['id']);
                db('content')->where(['contentid' => $val['id']])->update(array('url' => $url));
            }
            showmsg($modelarr[$modelid]['name'] . '更新完成！', '/admin/update_urls', 1);
        }
    }

}
